import fs from "fs-extra"
type ChapterInfo = {
  chapterNumber: number;
  title: string;
  content: string;
  wordCount: number;
}
export class ChapterParser {
  private filePath: string;
  constructor(filePath: string) {
    this.filePath = filePath;
  }

  parse() {
    const fileContent = fs.readFileSync(this.filePath, 'utf8');
    return this.parseChapters(fileContent);
  }

  parseChapters(fileContent: string) {
    const chapters: ChapterInfo[] = [];
    
    // 锲子章检测模式
    const prologuePatterns = [
      /楔子[\s\S]*?(?=第[\d一二三四五六七八九十百千万零]+[章回卷节集]|$)/i,
      /前言[\s\S]*?(?=第[\d一二三四五六七八九十百千万零]+[章回卷节集]|$)/i,
      /序章[\s\S]*?(?=第[\d一二三四五六七八九十百千万零]+[章回卷节集]|$)/i,
      /序言[\s\S]*?(?=第[\d一二三四五六七八九十百千万零]+[章回卷节集]|$)/i,
      /Prologue[\s\S]*?(?=Chapter\s+\d+|$)/i,
      /PROLOGUE[\s\S]*?(?=CHAPTER\s+\d+|$)/g
    ];

    // 普通章节检测模式
    const chapterPatterns = [
      /第[\d一二三四五六七八九十百千万零]+[章回卷节集][\s\S]*?(?=第[\d一二三四五六七八九十百千万零]+[章回卷节集]|$)/g,
      /Chapter\s+\d+[\s\S]*?(?=Chapter\s+\d+|$)/gi,
      /CHAPTER\s+\d+[\s\S]*?(?=CHAPTER\s+\d+|$)/g,
      /\d+\.[\s\S]*?(?=\d+\.|$)/g,
      /第\s*[\d一二三四五六七八九十百千万零]+\s*章[\s\S]*?(?=第\s*[\d一二三四五六七八九十百千万零]+\s*章|$)/g
    ];

    let hasPrologue = false;
    let prologueContent = '';

    // 检测锲子章
    for (const pattern of prologuePatterns) {
      const match = fileContent.match(pattern);
      if (match && match.length > 0) {
        prologueContent = match[0];
        hasPrologue = true;
        break;
      }
    }

    // 提取锲子章后的内容
    let remainingContent = fileContent;
    if (hasPrologue && prologueContent) {
      remainingContent = fileContent.replace(prologueContent, '');
      const prologueInfo = this.parseChapterInfo(prologueContent, 0, '锲子');
      chapters.push(prologueInfo);
    }

    // 解析普通章节
    let matches: string[] = [];
    for (const pattern of chapterPatterns) {
      const result = remainingContent.match(pattern);
      if (result && result.length > 0) {
        matches = result;
        break;
      }
    }

    matches.forEach((chapterContent, index) => {
      const chapterNumber = hasPrologue ? index + 1 : index + 1;
      const chapterInfo = this.parseChapterInfo(chapterContent, chapterNumber);
      chapters.push(chapterInfo);
    });

    return chapters;
  }

  parseChapterInfo(chapterContent: string, chapterNumber: number, defaultTitle?: string) {
    const lines = chapterContent.split('\n').filter(line => line.trim());
    let title = defaultTitle || (chapterNumber === 0 ? '锲子' : `第${chapterNumber}章`);
    let content = chapterContent;
    
    for (const line of lines) {
      const trimmedLine = line.trim();
      if (trimmedLine.match(/第[\d一二三四五六七八九十百千万零]+[章回卷节集]/)) {
        title = trimmedLine;
        content = chapterContent.replace(line, '').trim();
        break;
      } else if (trimmedLine.match(/楔子|前言|序章|序言/i)) {
        title = trimmedLine;
        content = chapterContent.replace(line, '').trim();
        break;
      } else if (trimmedLine.match(/Chapter\s+\d+/i)) {
        title = trimmedLine;
        content = chapterContent.replace(line, '').trim();
        break;
      } else if (trimmedLine.match(/Prologue/i)) {
        title = trimmedLine;
        content = chapterContent.replace(line, '').trim();
        break;
      }
    }
    
    return {
      chapterNumber,
      title: title.trim(),
      content: content.trim(),
      wordCount: content.length
    };
  }
}